/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.basic.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import me.zhengjie.modules.basic.domain.BasicAb01;
import me.zhengjie.modules.basic.domain.BasicAc01;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.basic.service.BasicAb01Service;
import me.zhengjie.modules.basic.domain.dto.BasicAb01QueryCriteria;
import me.zhengjie.modules.basic.mapper.BasicAb01Mapper;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
 * @description 服务实现
 * @author ly
 * @date 2025-04-25
 **/
@Service
@RequiredArgsConstructor
public class BasicAb01ServiceImpl extends ServiceImpl<BasicAb01Mapper, BasicAb01> implements BasicAb01Service {

    private final BasicAb01Mapper basicAb01Mapper;
    private final SequenceService sequenceService;
    @Resource
    BusinessLogService businessLogService;

    @Override
    public PageResult<BasicAb01> queryAll(BasicAb01QueryCriteria criteria, Page<Object> page) {
        return PageUtil.toPage(basicAb01Mapper.findAll(criteria, page));
    }

    @Override
    public List<BasicAb01> queryAll(BasicAb01QueryCriteria criteria) {
        return basicAb01Mapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(BasicAb01 resources) throws IOException {
        // 校验
        check(resources);
        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = objectMapper.convertValue(resources, Map.class);
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_DWJCXXXZ);
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);
        // 属组装
        Sequence sequence = new Sequence();
        sequence.setName("aab001");
        resources.setAab001(sequenceService.nextSequence(sequence));
        resources.setAae011(SecurityUtils.getCurrentUsername());
        resources.setAae036(DateUtil.getStringCurDateTime());
        basicAb01Mapper.insertAb01(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BasicAb01 resources) throws IOException {
        // 日志
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> param = objectMapper.convertValue(resources, Map.class);
        param.put("aaz002", sequenceService.nextSequence(new Sequence("aaz002")));
        param.put("aaa121", YHConstants.AAA121_DWJCXXbg);
        param.put("aae016", Constant.AAE016_1_SHTG);
        param.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        BusinessLog businessLog = BusinessLog.singleLog(param);
        businessLogService.save(businessLog);

        resources.setAae011(SecurityUtils.getCurrentUsername());
        resources.setAae036(DateUtil.getStringCurDateTime());
        basicAb01Mapper.updateByAab001(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<BigDecimal> ids) {
        basicAb01Mapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<BasicAb01> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BasicAb01 basicAb01 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("单位名称", basicAb01.getAab004());
            map.put("统一社会信用代码", basicAb01.getAab998());
            map.put("所属行业", basicAb01.getAab022());
            map.put("行业风险等级", basicAb01.getAaa149());
            map.put("单位类型", basicAb01.getAab019());
            map.put("法人姓名", basicAb01.getAab013());
            map.put("法人联系方式", basicAb01.getAae005());
            map.put("法人证件号码", basicAb01.getAac147());
            map.put("备注", basicAb01.getAae013());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 前置校验
     */
    public void check(BasicAb01 basicAb01) {
        // 非空校验
//        ValidateUtil.isEmptyThrowException(basicAb01.getAab998(),"统一社会信用代码");
        ValidateUtil.isEmptyThrowException(basicAb01.getAab004(),"单位名称");
        //证件号码重复校验
        BasicAb01 param = new BasicAb01();
        param.setAab998(basicAb01.getAab998());
        param.setAab004(basicAb01.getAab004());
        List<BasicAb01> ab01s = basicAb01Mapper.selectByAab998(param);
        if (!ValidateUtil.isEmpty(ab01s)) {
            throw new RuntimeException("【" + basicAb01.getAab004() + "】单位基础信息已存在，直接修改即可");
        }
    }

    @Override
    public List<BasicAb01> getUnitInfo(BasicAb01 criteria){
        return basicAb01Mapper.getUnitInfo(criteria);
    }

    @Override
    public List<BasicAb01> getAb01(BasicAb01 criteria){
        return basicAb01Mapper.getAb01(criteria);
    }
}
